﻿package cn.com.market.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

public class FileUtils {
	private String SDPATH;

	public String getSDPATH() {
		return SDPATH;
	}

	public FileUtils() {
		// 得到当前外部存储设备的目录
		// /SDCARD
		SDPATH = Environment.getExternalStorageDirectory() + "/";
	}

	/**
	 * 获得目录里面的所有文件名
	 * 
	 * @return
	 */
	public String[] getfilenames(String s) {
		List<String> l = new ArrayList<String>();
		String str[] = null;
		String path = SDPATH + s;
		File f = new File(path);
		if (f.isDirectory()) {
			File[] list = f.listFiles();

			for (int i = 0; i < list.length; i++) {
				if (list[i].isFile()) {
					l.add(list[i].getName().substring(0,
							list[i].getName().indexOf(".")));
				}
			}
		}
		str = new String[l.size()];
		for (int k = 0; k < l.size(); k++) {
			str[k] = l.get(k);
		}
		return str;
	}

	/**
	 * 在SD卡上创建文件
	 * 
	 * @throws IOException
	 */
	public File creatSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}

	/**
	 * 在SD卡上创建目录
	 * 
	 * @param dirName
	 */
	public File creatSDDir(String dirName) {
		File dir = new File(SDPATH + dirName);
		dir.mkdirs();
		return dir;
	}

	/**
	 * 判断SD卡上的文件夹是否存在
	 */
	public boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		return file.exists();
	}

	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 */
	public File write2SDFromInput(String path, String fileName,
			InputStream input) {
		File file = null;
		OutputStream output = null;
		try {
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			output = new FileOutputStream(file);
			byte buffer[] = new byte[4 * 1024];
			while ((input.read(buffer)) != -1) {
				output.write(buffer);
			}
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 */
	public File write2SDFromString(String path, String fileName, String input) {
		File file = null;
		BufferedWriter output = null;
		try {
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			output = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file)));
			output.write(input);
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	/**
	 * 该函数返回整形 -1：代表创建文件出错 0：代表创建文件成功 1：代表文件已经存在
	 */
	public int CreateFile(String path, String fileName, String str) {
		try {
			FileUtils fileUtils = new FileUtils();
			if (fileUtils.isFileExist(path + fileName)) {
				return 1;
			} else {
				File resultFile = fileUtils.write2SDFromString(path, fileName,
						str);
				if (resultFile == null) {
					return -1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
		}
		return 0;
	}

	/**
	 * 获取文件编码格式<br>
	 * ANSI:无格式定义;<br>
	 * Unicode Little Endian:前两个字节为FFFE;<br>
	 * Unicode Big Endian:前两字节为FEFF;
	 * UTF-8:前两字节为EFBB;
	 * @param path 文件路径
	 * @return
	 */
	public static String code(String path) {
		String code = "";
		try {
			File file = new File(path);
			java.io.BufferedInputStream bin = new java.io.BufferedInputStream(
					new java.io.FileInputStream(file));
			int p = (bin.read() << 8) + bin.read();
			code = null;
			switch (p) {
			case 0xefbb:
				code = "UTF-8";
				break;
			case 0xfffe:
				code = "Unicode";
				break;
			case 0xfeff:
				code = "UTF-16BE";
				break;
			default:
				code = "GBK";
			}
		} catch (Exception e) {
		}
		return code;
	}

	public String getFileContent(String path) {
		File f = new File(SDPATH + path);
		StringBuffer sb = new StringBuffer();
		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(f));
			BufferedReader in = new BufferedReader(read);
			String line;
			while ((line = in.readLine()) != null) {
				if (line.trim().length() > 0) {
					String str = line.trim() + "\n";
					sb.append(str);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (read != null) {
					read.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	public InputStream getFileStream(String path) {
		File f = new File(SDPATH + path);
		InputStream in = null;
		try {
			in = new FileInputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}

		return in;
	}

}